package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Base64;
import cj.utils.DateUtils;
import cj.web.admin.dao.AppUserInfoDAO;
import cj.web.admin.dao.ColumnsDAO;
import cj.web.admin.dao.CommentsDAO;
import cj.web.admin.dao.NewsDAO;
import cj.web.admin.domain.AppUserInfo;
import cj.web.admin.domain.Comments;
import cj.web.constants.CommentsReleaseStateEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/comments")
public class CommentsController extends BaseController{

	private static Logger logger = Logger.getLogger(CommentsController.class.getName());
	
	@Autowired
	private CommentsDAO commentsDao;
	@Autowired
	private ColumnsDAO columnsDao;
	@Autowired
	private NewsDAO newsDAO;
	@Autowired
	private AppUserInfoDAO appUserInfoDAO;
	
	@Get("show")
	public String show(Model model,Comments comments,@Param("currentPage") int currentPage) {
		//MemuEnum
		model.add("columnsAll", columnsDao.queryAll());
        model.add("MemuEnum",MemuEnum.lkggjplgl);
        model.add("CommentsReleaseStateEnum",EnumSet.allOf(CommentsReleaseStateEnum.class));
        model.add("currentPage",currentPage);//当前页
		return "/views/news/commentsList.vm";
	}

	@Post("getList")
	public String queryList(Comments comments) {
	
		List<Comments> arr = commentsDao.queryByComments(comments);
		
		for(Comments obj:arr)
		{
			AppUserInfo appUserInfo = appUserInfoDAO.queryById(obj.getReplyUserId());
			if(obj.getReplyName()==null || "".equals(obj.getReplyName()))
			{
				obj.setReplyName((appUserInfo.getName()==null || "".equals(appUserInfo.getName()))?appUserInfo.getDevicesId(): Base64.getFromBase64(appUserInfo.getName()));
			}
			
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = commentsDao.queryByCommentsSize(comments);
		
		jo.put("data", arr);
		jo.put("draw", comments.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id,@Param("currentPage") int currentPage)
	{
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkggjplgl);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/commentsEdit.vm";
	}
	
	@Get("editRelease")
	public String editRelease(Model model,@Param("id") int id,@Param("currentPage") int currentPage)
	{
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkggjplgl);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/commentsEditRelease.vm";
	}
	
	//回复
	@Post("updateEditReply")
	public String updateEditReply(Model model,Comments comments,HttpServletRequest request,@Param("currentPage") int currentPage)
	{
		//新增小编回复
		Comments obj = commentsDao.queryById(comments.getId());
		Comments g_obj = new Comments();
		
		g_obj.setIsAnonymous(1);
		g_obj.setContents(obj.getContents());
		g_obj.setCreateUser(returnSessionObj(request).getAccount());
		g_obj.setEditReply(obj.getReply());
		g_obj.setEditReplyUserId(obj.getReplyName());
		g_obj.setNewsId(obj.getNewsId());
		g_obj.setNewsTitle(obj.getNewsTitle());
		g_obj.setPraiseNum(0);
		g_obj.setReply(comments.getEditReply());
		g_obj.setReplyName("0");
		g_obj.setReplyUserId(0);
		g_obj.setUpdateUser(returnSessionObj(request).getAccount());
		g_obj.setReleaseState(CommentsReleaseStateEnum.YFB.getValue());
		
		commentsDao.create(g_obj);
		commentsDao.addCommentsById(comments.getNewsId());
		
		//当前评论审核通过
		comments.setEditReplyUserId(null);
		comments.setEditReply(null);
		comments.setIsAnonymous(0);
		comments.setReleaseState(CommentsReleaseStateEnum.YFB.getValue());
		comments.setUpdateUser(returnSessionObj(request).getAccount());
		commentsDao.update(comments);
		commentsDao.addCommentsById(comments.getNewsId());
		return "r:show?currentPage="+currentPage;
	}
	
	//修改回复
	@Post("updateRelease")
	public String updateRelease(Model model,Comments comments,HttpServletRequest request,@Param("currentPage") int currentPage)
	{
		comments.setReleaseState(CommentsReleaseStateEnum.YFB.getValue());
		comments.setUpdateUser(returnSessionObj(request).getAccount());
		commentsDao.update(comments);
		//commentsDao.addCommentsById(comments.getNewsId());
		return "r:show?currentPage="+currentPage;
	}
	
	//审核通过
	@Get("update")
	public String update(@Param("id") int id,HttpServletRequest request,@Param("currentPage") int currentPage)
	{
		Comments obj = commentsDao.queryById(id);
		obj.setIsAnonymous(0);
		obj.setReleaseState(CommentsReleaseStateEnum.YFB.getValue());
		obj.setUpdateUser(returnSessionObj(request).getAccount());
		commentsDao.update(obj);
		commentsDao.addCommentsById(obj.getNewsId());
		return "r:show?currentPage="+currentPage;
	}
	
	//取消
	@Get("channelRelease")
    public String channelRelease(@Param("id") int id,@Param("currentPage") int currentPage) {
		
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null)
		{
			commentsDao.updateByChannelRelease(id);
			commentsDao.subCommentsById(obj.getNewsId());
		}
		
		return "r:show?currentPage="+currentPage;
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id,@Param("currentPage") int currentPage)
	{
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null)
		{
			commentsDao.deleteById(id);
			if(CommentsReleaseStateEnum.YFB.getValue()==obj.getReleaseState())
			{
				commentsDao.subCommentsById(obj.getNewsId());
			}
			
		}
		return "r:show?currentPage="+currentPage;
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") int id) {
		
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null)
		{
			commentsDao.updateByStickTime(id);
		}
		
		return "r:show";
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") int id) {
		
		Comments obj = commentsDao.queryById(id);
		
		if(obj!=null)
		{
			commentsDao.updateByStickTimeIsNull(id);
		}
		
		return "r:show";
	}
}
